package com.fqgj.xjd.user.integration.impl;

import com.alibaba.fastjson.JSONObject;
import com.fqgj.common.utils.DateUtil;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.xjd.user.client.response.*;
import com.fqgj.xjd.user.integration.UserOcrService;
import com.fqgj.xjd.user.integration.ocr.HyperDocs;
import com.fqgj.xjd.user.integration.ocr.adapter.*;
import com.fqgj.xjd.user.integration.ocr.vo.KycTypeEnum;
import com.fqgj.xjd.user.integration.ocr.vo.OcrSuccessResponse;
import com.fqgj.xjd.user.integration.ocr.vo.OcrSuccessResult;
import org.apache.http.HttpStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author: moss
 * 2018-10-08
 */
@Service
public class UserOcrServiceImpl implements UserOcrService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserOcrServiceImpl.class);

    /**
     * aadhaar反面识别
     *
     * @param identityBackUrl
     */
    @Override
    public AadhaarBack ocrForAadhaarBack(String identityBackUrl) {
        String aadhaarJson = new HyperDocs().requestMethod(identityBackUrl
                , HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_AADHAAR);
        AadhaarBack aadhaarBack = null;
        if (aadhaarJson != null) {
            OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(aadhaarJson, OcrSuccessResponse.class);
            if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                String result = ocrSuccessResponse.getResult();
                List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                    if (ocrSuccessResult.getType().equals(KycTypeEnum.AADHAAR_BACK.getType())) {
                        String detail = ocrSuccessResult.getDetails();
                        aadhaarBack = JSONObject.parseObject(detail, AadhaarBack.class);
                        continue;
                    }
                }
            }
        }
        return aadhaarBack;
    }

    /**
     * aadhaar正面识别
     *
     * @param identityFrontUrl
     */
    @Override
    public AadhaarFrontBottom ocrForAadhaarFrontBottom(String identityFrontUrl) {
        String aadhaarJson = new HyperDocs().requestMethod(identityFrontUrl,
                HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_AADHAAR);
        AadhaarFrontBottom aadhaarFrontBottom = null;
        if (aadhaarJson != null) {
            OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(aadhaarJson, OcrSuccessResponse.class);
            if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                String result = ocrSuccessResponse.getResult();
                List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                    if (ocrSuccessResult.getType().equals(KycTypeEnum.AADHAAR_FRONT_BOTTOM.getType())) {
                        String detail = ocrSuccessResult.getDetails();
                        aadhaarFrontBottom = JSONObject.parseObject(detail, AadhaarFrontBottom.class);
                        continue;
                    }
                }
            }
        }
        if (aadhaarFrontBottom != null && StringUtils.isNotEmpty(aadhaarFrontBottom.getDob())) {
            aadhaarFrontBottom.setDob(aadhaarFrontBottom.getDob().replace("-", "/"));
        }
        return aadhaarFrontBottom;
    }

    /**
     * pan识别
     *
     * @param panCardUrl
     */
    @Override
    public Pan ocrForPan(String panCardUrl) {
        String panJson = new HyperDocs().requestMethod(panCardUrl,
                HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_PAN);
        Pan pan = null;
        try {
            if (panJson != null) {
                OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(panJson, OcrSuccessResponse.class);
                if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                    String result = ocrSuccessResponse.getResult();
                    List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                    for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                        if (ocrSuccessResult.getType().equals(KycTypeEnum.PAN.getType())
                                || ocrSuccessResult.getType().equals(KycTypeEnum.OLD_PAN.getType())) {
                            String details = ocrSuccessResult.getDetails();
                            PanAdapter panAdapter = JSONObject.parseObject(details, PanAdapter.class);
                            pan = panAdapter.doAdapter();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("ocrForPan exception, panJson:{}", panJson, e);
            return pan;
        }
        //日期统一
        if (pan != null) {
            String dateStr = pan.getDate();
            if (StringUtils.isNotEmpty(dateStr) && dateStr.matches("^[0-9]*$")) {
                DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
                try {
                    Date date = dateFormat.parse(dateStr);
                    dateStr = DateUtil.getDate(date, "dd/MM/yyyy");
                    pan.setDate(dateStr);
                } catch (ParseException e) {
                    LOGGER.error("ocrForPan ParseException", e);
                }
            }
        }
        return pan;
    }

    /**
     * passport正面识别
     *
     * @param passportFrontUrl
     * @return
     */
    @Override
    public OcrPassportFront ocrForPassportFront(String passportFrontUrl) {
        String passportFrontJson = new HyperDocs().requestMethod(passportFrontUrl
                , HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_PASSPORT);
        OcrPassportFront ocrPassportFront = null;
        try {
            if (StringUtils.isNotEmpty(passportFrontJson)) {
                OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(passportFrontJson, OcrSuccessResponse.class);
                if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                    String result = ocrSuccessResponse.getResult();
                    List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                    for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                        if (ocrSuccessResult.getType().equals(KycTypeEnum.PASSPORT_FRONT.getType())) {
                            String detail = ocrSuccessResult.getDetails();
                            PassportFrontAdapter passportFrontAdapter = JSONObject.parseObject(detail, PassportFrontAdapter.class);
                            ocrPassportFront = passportFrontAdapter.doAdapter();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("ocrForPassportFront exception, passportFrontJson:{}", passportFrontJson, e);
            return ocrPassportFront;
        }
        return ocrPassportFront;
    }

    /**
     * passport反面识别
     *
     * @param passportBackUrl
     * @return
     */
    @Override
    public OcrPassportBack ocrForPassportBack(String passportBackUrl) {
        String passportBackJson = new HyperDocs().requestMethod(passportBackUrl
                , HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_PASSPORT);
        OcrPassportBack ocrPassportBack = null;
        try {
            if (StringUtils.isNotEmpty(passportBackJson)) {
                OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(passportBackJson, OcrSuccessResponse.class);
                if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                    String result = ocrSuccessResponse.getResult();
                    List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                    for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                        if (ocrSuccessResult.getType().equals(KycTypeEnum.PASSPORT_BACK.getType())) {
                            String detail = ocrSuccessResult.getDetails();
                            PassportBackAdapter passportBackAdapter = JSONObject.parseObject(detail, PassportBackAdapter.class);
                            ocrPassportBack = passportBackAdapter.doAdapter();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("ocrForPassportBack exception, passportBackJson:{}", passportBackJson, e);
            return ocrPassportBack;
        }
        return ocrPassportBack;
    }

    /**
     * voterid正面识别
     *
     * @param voteridFrontUrl
     * @return
     */
    @Override
    public OcrVoteridFront ocrForVoteridFront(String voteridFrontUrl) {
        String voteridFrontJson = new HyperDocs().requestMethod(voteridFrontUrl
                , HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_KYC);
        OcrVoteridFront ocrVoteridFront = null;
        try {
            if (StringUtils.isNotEmpty(voteridFrontJson)) {
                OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(voteridFrontJson, OcrSuccessResponse.class);
                if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                    String result = ocrSuccessResponse.getResult();
                    List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                    for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                        if (ocrSuccessResult.getType().equals(KycTypeEnum.VOTERID_FRONT.getType())) {
                            String detail = ocrSuccessResult.getDetails();
                            VoteridFrontAdapter voteridFrontAdapter = JSONObject.parseObject(detail, VoteridFrontAdapter.class);
                            ocrVoteridFront = voteridFrontAdapter.doAdapter();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("ocrForVoteridFront exception, voteridFrontJson:{}", voteridFrontJson, e);
            return ocrVoteridFront;
        }
        return ocrVoteridFront;
    }

    /**
     * voterid反面识别
     *
     * @param voteridBackUrl
     * @return
     */
    @Override
    public OcrVoteridBack ocrForVoteridBack(String voteridBackUrl) {
        String voteridBackJson = new HyperDocs().requestMethod(voteridBackUrl
                , HyperDocs.InputType.IMAGE, HyperDocs.EndPoint.READ_KYC);
        OcrVoteridBack ocrVoteridBack = null;
        try {
            if (StringUtils.isNotEmpty(voteridBackJson)) {
                OcrSuccessResponse ocrSuccessResponse = JSONObject.parseObject(voteridBackJson, OcrSuccessResponse.class);
                if (ocrSuccessResponse.getStatusCode().equals(String.valueOf(HttpStatus.SC_OK))) {
                    String result = ocrSuccessResponse.getResult();
                    List<OcrSuccessResult> ocrSuccessResults = JSONObject.parseArray(result, OcrSuccessResult.class);
                    for (OcrSuccessResult ocrSuccessResult : ocrSuccessResults) {
                        if (ocrSuccessResult.getType().equals(KycTypeEnum.VOTERID_BACK.getType())) {
                            String detail = ocrSuccessResult.getDetails();
                            VoteridBackAdapter voteridBackAdapter = JSONObject.parseObject(detail, VoteridBackAdapter.class);
                            ocrVoteridBack = voteridBackAdapter.doAdapter();
                        }
                    }
                }
            }
        } catch (Exception e) {
            LOGGER.info("ocrForVoteridBack exception, voteridBackJson:{}", voteridBackJson, e);
            return ocrVoteridBack;
        }
        return ocrVoteridBack;
    }
}
